package com.zhouxiaoge.socket.base;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author 周明阳
 */
public class SocketService {

    public static void main(String[] args) {

        //1、创建一个服务端Socket
        ServerSocket serverSocket;
        try {
            serverSocket = new ServerSocket(8888);
            //2、调用accept()方法开始监听，等待客户端的连接
            while (true) {
                System.out.println("等待客户端请求。。。。");
                Socket accept = serverSocket.accept();
                //3、获取输入流，并读取客户端信息
                InputStream inputStream = accept.getInputStream();
                byte[] bytes = new byte[1024];
                int read = inputStream.read(bytes);
                String data = new String(bytes, 0, read);
                System.out.println("我是服务器，客户端说：" + data);
                accept.shutdownInput();
                OutputStream outputStream = accept.getOutputStream();
                outputStream.write(data.toUpperCase().getBytes());
                outputStream.close();
                inputStream.close();
                accept.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
